﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Golf.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace Golf.DAL
{
    /// <summary>
    /// 数据访问类:Golf_User
    /// </summary>
    public partial class Golf_User : IGolf_User
    {
        public Golf_User()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id", "Golf_User");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            int result = DbHelperSQL.RunProcedure("Golf_User_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(Golf.Model.Golf_User model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@UserType", SqlDbType.Int,4),
					new SqlParameter("@FristName", SqlDbType.NVarChar,100),
					new SqlParameter("@LastName", SqlDbType.NVarChar,100),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,32),
					new SqlParameter("@Email", SqlDbType.NVarChar,100),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@RegisterTime", SqlDbType.DateTime),
					new SqlParameter("@LoginTime", SqlDbType.DateTime),
					new SqlParameter("@Endtime", SqlDbType.DateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.UserType;
            parameters[2].Value = model.FristName;
            parameters[3].Value = model.LastName;
            parameters[4].Value = model.PassWord;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.State;
            parameters[7].Value = model.RegisterTime;
            parameters[8].Value = model.LoginTime;
            parameters[9].Value = model.Endtime;

            DbHelperSQL.RunProcedure("Golf_User_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public bool Update(Golf.Model.Golf_User model)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@UserType", SqlDbType.Int,4),
					new SqlParameter("@FristName", SqlDbType.NVarChar,100),
					new SqlParameter("@LastName", SqlDbType.NVarChar,100),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,32),
					new SqlParameter("@Email", SqlDbType.NVarChar,100),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@RegisterTime", SqlDbType.DateTime),
					new SqlParameter("@LoginTime", SqlDbType.DateTime),
					new SqlParameter("@Endtime", SqlDbType.DateTime)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.UserType;
            parameters[2].Value = model.FristName;
            parameters[3].Value = model.LastName;
            parameters[4].Value = model.PassWord;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.State;
            parameters[7].Value = model.RegisterTime;
            parameters[8].Value = model.LoginTime;
            parameters[9].Value = model.Endtime;

            DbHelperSQL.RunProcedure("Golf_User_Update", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            DbHelperSQL.RunProcedure("Golf_User_Delete", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Golf_User ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Golf.Model.Golf_User GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            Golf.Model.Golf_User model = new Golf.Model.Golf_User();
            DataSet ds = DbHelperSQL.RunProcedure("Golf_User_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserType"] != null && ds.Tables[0].Rows[0]["UserType"].ToString() != "")
                {
                    model.UserType = int.Parse(ds.Tables[0].Rows[0]["UserType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FristName"] != null && ds.Tables[0].Rows[0]["FristName"].ToString() != "")
                {
                    model.FristName = ds.Tables[0].Rows[0]["FristName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LastName"] != null && ds.Tables[0].Rows[0]["LastName"].ToString() != "")
                {
                    model.LastName = ds.Tables[0].Rows[0]["LastName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PassWord"] != null && ds.Tables[0].Rows[0]["PassWord"].ToString() != "")
                {
                    model.PassWord = ds.Tables[0].Rows[0]["PassWord"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Email"] != null && ds.Tables[0].Rows[0]["Email"].ToString() != "")
                {
                    model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["State"] != null && ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RegisterTime"] != null && ds.Tables[0].Rows[0]["RegisterTime"].ToString() != "")
                {
                    model.RegisterTime = DateTime.Parse(ds.Tables[0].Rows[0]["RegisterTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LoginTime"] != null && ds.Tables[0].Rows[0]["LoginTime"].ToString() != "")
                {
                    model.LoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LoginTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Endtime"] != null && ds.Tables[0].Rows[0]["Endtime"].ToString() != "")
                {
                    model.Endtime = DateTime.Parse(ds.Tables[0].Rows[0]["Endtime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,UserType,FristName,LastName,PassWord,Email,State,RegisterTime,LoginTime,Endtime ");
            strSql.Append(" FROM Golf_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,UserType,FristName,LastName,PassWord,Email,State,RegisterTime,LoginTime,Endtime ");
            strSql.Append(" FROM Golf_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "Golf_User";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;// 返回记录总数, 非 0 值则返回 
            parameters[5].Value = 0;//设置排序类型, 非 0 值则降序
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  Method
    }
}

